//
//  AFNManager.m
//  Three
//
//  Created by 夜猫子 on 2017/3/22.
//  Copyright © 2017年 夜猫子. All rights reserved.
//

#import "AFNManager.h"

@implementation AFNManager

+ (instancetype)sharedManager{
    
    static AFNManager *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[AFNManager alloc]initWithBaseURL:nil ];
        
        tool.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain",@"text/html", nil];
        
    });
    return tool;
}


/**
 发起网络请求
 
 @param strURL URL字符串
 @param method 请求方式
 @param parameters 请求参数字典
 @param callBlock 完成回调BLock
 */
- (void)requestWithURL:(NSString *)strURL
                method:(NSString *)method
            parameters:(id)parameters
             callBlock:(void(^)(id response))callBlock{
    
    if ([method isEqualToString:@"GET"]) {
        
        [self GET:strURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            callBlock(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            callBlock(nil);
            
        }];
        
    }
    if ([method isEqualToString:@"POST"]) {
        
        [self POST:strURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            callBlock(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            callBlock(nil);
        }];
        
    }
    
}


@end
